//
//  WBTabBar.m
//  weiboOC
//
//  Created by 谢佳芯 on 2017/9/1.
//  Copyright © 2017年 xjx. All rights reserved.
//

#import "WBTabBar.h"
#import <Masonry.h>

@interface WBTabBar ()
//撰写按钮
@property (nonatomic, weak) UIButton *composeBtn;


@end

@implementation WBTabBar


- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI
{
    //创建按钮
    UIButton *composeBtn = [[UIButton alloc] init];
    
    //设置背景图片
    [composeBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [composeBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    //设置图片
    [composeBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [composeBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    
    self.composeBtn = composeBtn;
    
    [self addSubview:composeBtn];
    [composeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    
    //添加点击事件
    [composeBtn addTarget:self action:@selector(composeBuuttonClick:) forControlEvents:UIControlEventTouchUpInside];

}
#pragma mark - 按钮点击事件
- (void)composeBuuttonClick:(UIButton *)button
{
    self.composeBtnClickClosure();
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat btnW = [UIScreen mainScreen].bounds.size.width / 5;
    CGFloat index = 0;
    for (UIView *subView in self.subviews)
    {
        //判断是不是tabBarBTN
        if ([subView class] == NSClassFromString(@"UITabBarButton"))
        {
            subView.frame = CGRectMake(index * btnW, subView.frame.origin.y, btnW, subView.frame.size.height);
            
            index += 1;
            
            // 如果是第2个按钮，再往后面移动一个位置
            if(index == 2)
            {
                index += 1;
            }
        }
    }
    
}

@end
